<?php
namespace app\admin\controller;


use app\admin\common\controller\Base;
use app\admin\common\model\Temp as TempModel;
use app\admin\common\model\TempCate;
use app\admin\common\model\TempColor;
use think\facade\Request;

class Temp extends Base
{
    /*
     * 模板列表
     */
    public function tempList(TempModel $tempModel)
    {
        $datas = $tempModel->getPageList();

        $this->assign('datas',$datas);
        return $this->fetch('tempList');
    }

    /*
     * 模板添加
     */
    public function addTemp(TempCate $tempCate, TempColor $tempColor)
    {
        $cateList = $tempCate->getInfoList();

        if ($cateList) {
            $this->assign('cateList',$cateList);
        }

        $colorList = $tempColor->getInfoList();

        if ($colorList) {
            $this->assign('colorList',$colorList);
        }

        return $this->fetch('addTemp');
    }

    /*
     * 添加模板操作
     */
    public function addTempDo(TempModel $tempModel)
    {
        $params = Request::param();
        $rule = 'app\admin\validate\AddTemp';

        $result = $this->validate($params,$rule);

        if (true !== $result) {
            $this->error($result);
        }


        //判断上传图片不为空
        if (empty(Request::file())) {
            $this->error('图片不能为空');
        }
        //获取上传的图片信息
        $file = Request::file('temp_img'); //获取file对象

        //文件信息验证与上传到服务器
        $info = $file->validate([
            'size'=>500000000,  //文件大小
            'ext'=>'jpeg,jpg,png,gif'  //文件扩展名
        ])->move('uploads/');

        if ($info) {
            $params['temp_img'] = $info->getSaveName();
        } else {
            $this->error($file->getError(),'addTemp');
        }


        $res = $tempModel->addTemp($params);

        if ($res) {
            $this->success('添加成功','tempList');
        }

        $this->error('添加失败');
    }

    /*
     * 模板编辑
     */
    public function editTemp(TempCate $tempCate, TempColor $tempColor, TempModel $tempModel)
    {
        $cateList = $tempCate->getInfoList();
        if ($cateList) {
            $this->assign('cateList',$cateList);
        }

        $colorList = $tempColor->getInfoList();
        if ($colorList) {
            $this->assign('colorList',$colorList);
        }

        $id = Request::param('id');

        if (empty($id)) {
            $this->error('数据ID丢失');
        }

        $info = $tempModel->getInfoById($id);

        $this->assign('info',$info);
        return $this->fetch('editTemp');
    }

    /*
     * 编辑模板操作
     */
    public function editTempDo(TempModel $tempModel)
    {
        $params = Request::param();
        $rule = 'app\admin\validate\EditTemp';
        $result = $this->validate($params,$rule);

        if (true !== $result) {
            $this->error($result);
        }

        //如果上传图片不为空,再生成$params['temp_img'],否则,不生成
        //生成的$param['temp_img']用于修改TempModel中temp_img的值
        if (!empty(Request::file())) {

            //删除id对应的图片
            $tempModel->delTempImg($params['id']);

            $file = Request::file('temp_img');
            $info = $file->validate([
                'size'  =>  500000000,
                'ext'   =>  'jpeg,jpg,png,gif',
            ])->move('uploads/');

            if ($info) {
                $params['temp_img'] = $info->getSaveName();
            } else {
                $this->error($file->getError());
            }
        }


        $res = $tempModel->editTemp($params);

        if ($res) {
            $this->success('更新成功','tempList');
        }

        $this->error('更新失败');
    }

    /*
     * 删除模板操作
     */
    public function delTemp(TempModel $tempModel)
    {
        $id = Request::param('id');
        if (empty($id)) {
            $this->error('数据ID丢失');
        }

        $res = $tempModel->delTemp($id);

        if ($res) {
            $this->success('删除成功','tempList');
        }

        $this->error('删除失败');
    }
}